package com.peaker.advertisingpro.utils

import android.content.Context
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.peaker.advertisingpro.bean.ResultBean
import com.peaker.advertisingpro.bean.SetData
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type


/**
 *  @ 作者 CODE_LYF
 *  @ 时间   2021/4/18.
 *  代码人生
 */
object JsonObjectUtils {


    inline fun <reified T> getJsonObject(json: String): T {
        val type = object : TypeToken<ResultBean<T>>() {}.type
//        val data = Gson().fromJson<ResultBean<T>>(json, getType(ResultBean::class.java, getType(T::class.java,String::class.java)))
        val data = Gson().fromJson<ResultBean<T>>(json, type)
        return data.data
    }


    /**
     * 获取asset的文件
     * @param jsonFile String
     * @param mContext Context
     * @return T
     */
    fun <T> getJsonFileObject(jsonFile: String, mContext: Context): T {
        val json = FileUtils.getJson(mContext, jsonFile)
        val type = object : TypeToken<T>() {}.type
        return Gson().fromJson(json, type)
    }

    /**
     * 获取 内存地址的文件
     * @param jsonFile String
     * @return T
     */
    fun <T> getJsonFileObject(jsonFile: String): T {
        var json = FileUtils.getJsonFromFile(jsonFile)
        val type = object : TypeToken<T>() {}.type
        val data = Gson().fromJson<T>(json, type)
        return data
    }

    /**
     * 将json 转换为对象
     * @param json String
     * @return T
     */
    fun <T> getObjByJson(json: String): T {
        val type = object : TypeToken<T>() {}.type
        return Gson().fromJson<T>(json, type)
    }


    fun getType(raw: Class<*>, vararg args: Type) = object : ParameterizedType {
        override fun getRawType(): Type = raw
        override fun getActualTypeArguments(): Array<out Type> = args
        override fun getOwnerType(): Type? = null
    }

}